import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import geopandas as gpd
# list of the police files
def crime_source(police, year, months):
crime_pth = []
for p in police:
for y in year:
for m in months:
crime_pth.append('C:\\Datasets\\Crime_UK_2020\\'+y+'-'+m+'-'+p+'-street.csv')
return crime_pth
def map_source(maps):
map_pth = 'C:\\Datasets\\Crime_UK_2020\\'+maps[0]+'.png'
return map_pth
def coordinates(maps):
coord = pd.read_csv(coord_path,sep='\t')
return coord
def MapBox(coord):
long1 = coord[coord.name==maps[0]].iloc[:,1].values[0]
long2 = coord[coord.name==maps[0]].iloc[:,2].values[0]
lat1 = coord[coord.name==maps[0]].iloc[:,3].values[0]
lat2 = coord[coord.name==maps[0]].iloc[:,4].values[0]
MapBox2 = (long1,long2,lat1,lat2)
return MapBox2
def crime_merge(crime_pth):
crime_all=pd.DataFrame(columns=['Crime ID','Month','Reported by','Falls within','Longitude',
'Latitude','Location','LSOA code','LSOA name','Crime type',
'Last outcome category','Context'])
for i in range(len(crime_pth)):
# print(i,crime_pth[i] )
crime_all=crime_all.append(pd.read_csv(crime_pth[i]))
return crime_all
def map_read(map_pth):
map_img =plt.imread(map_pth)
return map_img
def crime_allf():
crime_all1 = crime_all[crime_all.Location != 'No Location']
print('merged data with location data dimension:',crime_all1.shape)
crime_all1 = crime_all1[(crime_all1.Longitude>=long1) & (crime_all1.Longitude<=long2) & (crime_all1.Latitude>=lat1) &
(crime_all1.Latitude<=lat2)]
print('in the box data dimension:',crime_all1.shape)
return crime_all1
def MapBox2f(MapBox2):
MapBox = ((crime_all.Longitude.min(),crime_all.Longitude.max(),
crime_all.Latitude.min(), crime_all.Latitude.max()))
print(f'Data generated MapBox coordinates:',MapBox,f'\nMy predifined MapBox coordinates:' ,MapBox2)
def MxCrime():
print(crime_all.groupby('Crime type').count()[['Month']])
loc_freq = crime_all.groupby(['Longitude','Latitude']).count()[['Month']]
print('\nMaximum number of crime in one scene:', loc_freq.max()[0])
mflong = loc_freq[loc_freq.Month==loc_freq.max()[0]].reset_index().iloc[0,0]
mflat = loc_freq[loc_freq.Month==loc_freq.max()[0]].reset_index().iloc[0,1]
print(f'The coordinates of this crime scene:',mflong,mflat)
max_crime = (mflong, mflat)
# here I create a table which contains the most frequent crime name and ratio
df1 = crime_all.groupby(['Longitude','Latitude']).count()[['Month']].reset_index()
df2 = crime_all.groupby(['Longitude','Latitude','Crime type']).count()[['Month']].reset_index()
df3 = df2.groupby(['Longitude','Latitude']).max()[['Month']].reset_index()
df4 = pd.merge(df2, df3, how='inner', left_on=['Longitude','Latitude','Month'], right_on = ['Longitude','Latitude','Month'])
df4.drop_duplicates(subset =["Longitude","Latitude"], keep = 'first', inplace = True)
df4 = df4.rename(columns={"Month": "MaxCount"})
df5 = pd.merge(df1, df4, how='inner', left_on=['Longitude','Latitude'], right_on = ['Longitude','Latitude'])
df5['maxRatio%'] = 100*df5["MaxCount"]/df5["Month"]
df["maxRatio%"] = df["maxRatio%"].astype(int)
df5 = df5.rename(columns={"Month": "Count"})
return max_crime, loc_freq, df5
def map_display(max_crime,loc_freq):
mflong=max_crime[0]
mflat=max_crime[1]
#fig, ax = plt.subplots(figsize = (30,30))
fig, ax = plt.subplots(figsize = ar)
loc_freq = crime_all.groupby(['Longitude','Latitude']).count()[['Month']].reset_index()
ax.scatter(loc_freq.Longitude, loc_freq.Latitude, zorder=1, alpha= 0.8, c='r', s=loc_freq.Month*20)
ax.scatter(mflong, mflat, alpha= 0.8, zorder=5, c='y', s=1000)
ax.set_title('Plotting Crime Data in '+maps[0].capitalize(), fontsize=40)
ax.set_xlim(MapBox2[0],MapBox2[1])# not necessary at the moment
ax.set_ylim(MapBox2[2],MapBox2[3])# not necessary at the moment
ax.imshow(map_img, zorder=0, extent = MapBox2, aspect= 'auto')
plt.show()
#df3 = df2.pivot_table('Month', ['Longitude', 'Latitude'], 'Crime type')
#df3 = df3.fillna(0)
#df3['combined']= df3.iloc[:,2:].values.tolist()
#df4 = df3.reset_index()
#df4.loc[:,['Longitude','Latitude','combined']]
#df2.groupby(['Longitude','Latitude']).max()[['Month']].reset_index()
#df5.head(15)
pol_list = ['avon-and-somerset','bedfordshire','btp','cambridgeshire','cheshire','city-of-london',
'cleveland','cumbria','derbyshire','devon-and-cornwall','dorset','durham',
'dyfed-powys','essex','gloucestershire','gwent','hampshire','hertfordshire',
'humberside','kent','lancashire','leicestershire','lincolnshire','merseyside',
'metropolitan','norfolk','northamptonshire','northern-ireland','northumbria','north-wales',
'north-yorkshire','nottinghamshire','south-wales','south-yorkshire','staffordshire','suffolk',
'surrey','sussex','thames-valley','warwickshire','west-mercia','west-midlands',
'west-yorkshire','wiltshire']
# police files
police = ['hampshire'] # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
# map name
maps = ['basingstoke']
ar = (30,20) # aspect ratio - if the map seems to distorted, use this to correct it (figsize parameter)
# fix file storing the coordinates of pictures
coord_path = 'C:\\Datasets\\Crime_UK_2020\\map_coordinatesreadme.txt'
crime_pth = crime_source(police, year, months)
map_pth = map_source(maps)
coord = coordinates(maps)
MapBox2 = MapBox(coord)
long1 = MapBox2[0]
long2 = MapBox2[1]
lat1 = MapBox2[2]
lat2 = MapBox2[3]
crime_all = crime_merge(crime_pth)
map_img = map_read(map_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
map_display(max_crime,loc_freq)
# police files
police = ['thames-valley'] # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
# map name
maps = ['newbury2']
ar = (30,20) # aspect ratio - if the map seems to distorted, use this to correct it (figsize parameter)
# fix file storing the coordinates of pictures
coord_path = 'C:\\Datasets\\Crime_UK_2020\\map_coordinatesreadme.txt'
crime_pth = crime_source(police, year, months)
map_pth = map_source(maps)
coord = coordinates(maps)
MapBox2 = MapBox(coord)
long1 = MapBox2[0]
long2 = MapBox2[1]
lat1 = MapBox2[2]
lat2 = MapBox2[3]
crime_all = crime_merge(crime_pth)
map_img = map_read(map_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
map_display(max_crime,loc_freq)
# police files
police = ['thames-valley','metropolitan','btp','surrey'] # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
# map name
maps = ['ashford']
ar = (30,30) # aspect ratio - if the map seems to distorted, use this to correct it (figsize parameter)
# fix file storing the coordinates of pictures
coord_path = 'C:\\Datasets\\Crime_UK_2020\\map_coordinatesreadme.txt'
crime_pth = crime_source(police, year, months)
map_pth = map_source(maps)
coord = coordinates(maps)
MapBox2 = MapBox(coord)
long1 = MapBox2[0]
long2 = MapBox2[1]
lat1 = MapBox2[2]
lat2 = MapBox2[3]
crime_all = crime_merge(crime_pth)
map_img = map_read(map_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
map_display(max_crime,loc_freq)
# police files
police = ['thames-valley'] # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
# map name
maps = ['didcot']
ar = (30,20) # aspect ratio - if the map seems to distorted, use this to correct it (figsize parameter)
# fix file storing the coordinates of pictures
coord_path = 'C:\\Datasets\\Crime_UK_2020\\map_coordinatesreadme.txt'
crime_pth = crime_source(police, year, months)
map_pth = map_source(maps)
coord = coordinates(maps)
MapBox2 = MapBox(coord)
long1 = MapBox2[0]
long2 = MapBox2[1]
lat1 = MapBox2[2]
lat2 = MapBox2[3]
crime_all = crime_merge(crime_pth)
map_img = map_read(map_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
map_display(max_crime,loc_freq)
import folium
from folium import plugins
import ipywidgets
# conda install -c conda-forge geocoder
import geocoder
import geopy
from vega_datasets import data as vds
address = geocoder.osm('Didcot, UK')
long1 = address.lng -0.12
long2 = address.lng +0.12
lat1 = address.lat -0.1
lat2 = address.lat +0.1
MapBox2 = (long1,long2,lat1,lat2)
MapBox2
# police files
police = pol_list # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
crime_pth = crime_source(police, year, months)
crime_all = crime_merge(crime_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
#df = loc_freq.reset_index()
df = df5
# create map
map_crime = folium.Map(location=[address.lat,address.lng], zoom_start=13)
folium.raster_layers.TileLayer('Open Street Map').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Terrain').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Toner').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Watercolor').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Positron').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Dark_Matter').add_to(map_crime)
# add layer control to show different maps
folium.LayerControl().add_to(map_crime)
# plot crime locations
df.apply(lambda row: folium.CircleMarker(location=[row['Latitude'], row['Longitude']],radius=row['Count'],
color='black', fill_color='red',alpha = 0.8,
# popup=row['MaxCount']).add_to(map_crime), axis=1)
popup= 'Count:' + str(row.loc['Count']) + '<br />Most Common:' +
row.loc['Crime type'] + '<br />Ratio:' + str(row.loc['maxRatio%'])).add_to(map_crime), axis=1)
# display map
map_crime
address = geocoder.osm('Newbury, UK')
long1 = address.lng -0.12
long2 = address.lng +0.12
lat1 = address.lat -0.1
lat2 = address.lat +0.1
MapBox2 = (long1,long2,lat1,lat2)
# police files
police = pol_list # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
crime_pth = crime_source(police, year, months)
crime_all = crime_merge(crime_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
#df = loc_freq.reset_index()
df = df5
# create map
map_crime = folium.Map(location=[address.lat,address.lng], zoom_start=13)
folium.raster_layers.TileLayer('Open Street Map').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Terrain').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Toner').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Watercolor').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Positron').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Dark_Matter').add_to(map_crime)
# add layer control to show different maps
folium.LayerControl().add_to(map_crime)
# plot crime locations
df.apply(lambda row: folium.CircleMarker(location=[row['Latitude'], row['Longitude']],radius=row['Count'],
color='black', fill_color='red',alpha = 0.8,
# popup=row['MaxCount']).add_to(map_crime), axis=1)
popup= 'Count:' + str(row.loc['Count']) + '<br />Most Common:' +
row.loc['Crime type'] + '<br />Ratio:' + str(row.loc['maxRatio%'])).add_to(map_crime), axis=1)
# display map
map_crime
address = geocoder.osm('Basingstoke, UK')
long1 = address.lng -0.12
long2 = address.lng +0.12
lat1 = address.lat -0.1
lat2 = address.lat +0.1
MapBox2 = (long1,long2,lat1,lat2)
# police files
police = pol_list # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
crime_pth = crime_source(police, year, months)
crime_all = crime_merge(crime_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
#df = loc_freq.reset_index()
df = df5
# create map
map_crime = folium.Map(location=[address.lat,address.lng], zoom_start=13)
folium.raster_layers.TileLayer('Open Street Map').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Terrain').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Toner').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Watercolor').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Positron').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Dark_Matter').add_to(map_crime)
# add layer control to show different maps
folium.LayerControl().add_to(map_crime)
# plot crime locations
df.apply(lambda row: folium.CircleMarker(location=[row['Latitude'], row['Longitude']],radius=row['Count'],
color='black', fill_color='red',alpha = 0.8,
# popup=row['MaxCount']).add_to(map_crime), axis=1)
popup= 'Count:' + str(row.loc['Count']) + '<br />Most Common:' +
row.loc['Crime type'] + '<br />Ratio:' + str(row.loc['maxRatio%'])).add_to(map_crime), axis=1)
# display map
map_crime
address = geocoder.osm('TW15 1JB, UK')
long1 = address.lng -0.12
long2 = address.lng +0.12
lat1 = address.lat -0.1
lat2 = address.lat +0.1
MapBox2 = (long1,long2,lat1,lat2)
# police files
police = pol_list # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
crime_pth = crime_source(police, year, months)
crime_all = crime_merge(crime_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
#df = loc_freq.reset_index()
df = df5
# create map
map_crime = folium.Map(location=[address.lat,address.lng], zoom_start=13)
folium.raster_layers.TileLayer('Open Street Map').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Terrain').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Toner').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Watercolor').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Positron').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Dark_Matter').add_to(map_crime)
# add layer control to show different maps
folium.LayerControl().add_to(map_crime)
# plot crime locations
df.apply(lambda row: folium.CircleMarker(location=[row['Latitude'], row['Longitude']],radius=row['Count'],
color='black', fill_color='red',alpha = 0.8,
# popup=row['MaxCount']).add_to(map_crime), axis=1)
popup= 'Count:' + str(row.loc['Count']) + '<br />Most Common:' +
row.loc['Crime type'] + '<br />Ratio:' + str(row.loc['maxRatio%'])).add_to(map_crime), axis=1)
# display map
map_crime
address = geocoder.osm('Wells, UK')
long1 = address.lng -0.12
long2 = address.lng +0.12
lat1 = address.lat -0.1
lat2 = address.lat +0.1
MapBox2 = (long1,long2,lat1,lat2)
# police files
police = pol_list # the police identifier in the police files
year=['2020']
months = ['01','02','03','04']
crime_pth = crime_source(police, year, months)
crime_all = crime_merge(crime_pth)
crime_all = crime_allf()
MapBox2f(MapBox2)
max_crime, loc_freq, df5 = MxCrime()
#df = loc_freq.reset_index()
df = df5
# create map
map_crime = folium.Map(location=[address.lat,address.lng], zoom_start=13)
folium.raster_layers.TileLayer('Open Street Map').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Terrain').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Toner').add_to(map_crime)
folium.raster_layers.TileLayer('Stamen Watercolor').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Positron').add_to(map_crime)
#folium.raster_layers.TileLayer('CartoDB Dark_Matter').add_to(map_crime)
# add layer control to show different maps
folium.LayerControl().add_to(map_crime)
# plot crime locations
df.apply(lambda row: folium.CircleMarker(location=[row['Latitude'], row['Longitude']],radius=row['Count'],
color='black', fill_color='red',alpha = 0.8,
# popup=row['MaxCount']).add_to(map_crime), axis=1)
popup= 'Count:' + str(row.loc['Count']) + '<br />Most Common:' +
row.loc['Crime type'] + '<br />Ratio:' + str(row.loc['maxRatio%'])).add_to(map_crime), axis=1)
# display map
map_crime